package com.huika.renrench.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseListFrag;
import com.huika.renrench.control.main.epship.acty.EnProjectDetailActivity;
import com.huika.renrench.control.main.epship.adap.EntrepreneurshipProjectAdapter;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.EnshipProject;
import com.huika.renrench.support.bean.LocationBean;
import com.huika.renrench.support.bean.LocationBean.MyLocationListener;
import com.huika.renrench.utils.GaodeMapHelper;
/**
 * tab页找项目
 * @author fanxing 创建于 Mar 30, 2015
 */
/**
 * tab页找项目
 * @author fanxing 创建于 Mar 30, 2015
 */
public class FindProjectsFragment extends BaseListFrag<EnshipProject> implements MyLocationListener {
	
	private FormResultRequest<List<EnshipProject>> resultRequest;
	private GaodeMapHelper gaodeMapHelper;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.frag_find_project, container, false);
		initPullListView(root);
		
		//标题栏部分
		TextView tv_title = (TextView) root.findViewById(R.id.title);
		tv_title.setText(R.string.near_projects);
		root.findViewById(R.id.right).setVisibility(View.GONE);
		root.findViewById(R.id.left).setVisibility(View.GONE);
				
		initLoadData();
		return root;
	}
	
	@Override
	protected void initLoadData() {
		initRequest();
		if(!GlobalApp.getInstance().isLocated()){
			startLocation();
		}else{
			super.initLoadData();
		}
	}

	private void startLocation() {
		hideOverLay();
		showLoadingDialog("定位中");
		gaodeMapHelper = new GaodeMapHelper(getActivity());
		gaodeMapHelper.setmLocationListener(this);
		gaodeMapHelper.start();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if(gaodeMapHelper != null){
			gaodeMapHelper.stop();
		}
	}
	
	@Override
	protected void initPullListView(View root) {
		mPullRefreshListView = (PullToRefreshListView) root.findViewById(R.id.pull_refresh_list);
		
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
				reloadData();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
				CURRENT_PAGE++;
				//加载下一页
				ajaxParams.putCommonTypeParam("pageNo", CURRENT_PAGE);
				executeRequest(resultRequest);
			}
		});
		mPullRefreshListView.setMode(Mode.PULL_FROM_START);
		adapter = new EntrepreneurshipProjectAdapter(getActivity());
		mPullRefreshListView.setAdapter(adapter);
		mPullRefreshListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ListView listView = (ListView) parent;
				int hc = listView.getHeaderViewsCount();
				int realP = position - hc;
				if (realP>=0 && realP<adapter.getCount()) {
					cancelRequest(this);
					mPullRefreshListView.onRefreshComplete();
					
					EnshipProject addressBean = (EnshipProject) adapter.getItem(realP);
					Intent show = new Intent(getActivity(), EnProjectDetailActivity.class);
					show.putExtra(EnProjectDetailActivity.PROJECT_TAG, addressBean);
					startActivity(show);
				}
			}
			
		});
	}

	@Override
	public void reloadData() {
		CURRENT_PAGE = 1;
		//刷新
		LocationBean locationBean = LocationBean.getLastLocation();
		if(locationBean != null){
			ajaxParams.putCommonTypeParam("pageNo", CURRENT_PAGE);
			ajaxParams.putCommonTypeParam("longitude", locationBean.longtitude);
			ajaxParams.putCommonTypeParam("latitude", locationBean.latitude);
			executeRequest(resultRequest);
		}else{
			dismissLoadingDialog();
			startLocation();
			return;
		}
	}

	@Override
	public void initRequest(){
		if(resultRequest != null)
			return;
		
		ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("pageSize", PAGE_SIZE);
		ajaxParams.putStringTypeParam("userId", "0");
		resultRequest = new FormResultRequest(UrlConstants.URL_FIND_PROJECTS, this, this, 
				new TypeToken<RequestResult<List<EnshipProject>>>() {}.getType());
		resultRequest.setRequestParams(ajaxParams);
	}

	/**
	 * 定位成功
	 * @param locationBean
	 * @author FAN 创建于Mar 31, 2015
	 */
	@Override
	public void onLocation(LocationBean locationBean) {
		if(!TextUtils.isEmpty(locationBean.province)){
			GlobalApp.getInstance().setLocated(true);
			initLoadData();
		}else{//定位失败
			showToastMsg("定位失败");
			dismissLoadingDialog();
		}
	}

	@Override
	protected View inflaterView(LayoutInflater inflater, ViewGroup container,
			Bundle bundle) {
		// TODO Auto-generated method stub
		return null;
	}

}
